package com.dingreading.cloud.subject.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.IpUtil;
import com.dingreading.cloud.subject.dto.BookDto;
import com.dingreading.cloud.subject.dto.SubjectLessonDto;
import com.dingreading.cloud.subject.dto.SubjectLessonVideoDto;
import com.dingreading.cloud.subject.dto.SubjectLessonVideoListDto;
import com.dingreading.cloud.subject.entity.SubjectLessonLog;
import com.dingreading.cloud.subject.entity.Subjects;
import com.dingreading.cloud.subject.service.SubjectLessonLogService;
import com.dingreading.cloud.subject.service.SubjectLessonService;
import com.dingreading.cloud.subject.service.SubjectLessonVideoService;
import com.dingreading.cloud.subject.service.SubjectsService;
import com.dingreading.cloud.subject.util.CdnUrlUtil;
import com.dingreading.cloud.subject.util.SubjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RefreshScope
@RestController
@RequestMapping("/front/subjectLesson")
@Api(tags = "门店-学科课次管理")
public class FrontSubjectLessonController {

    @Value("${subjectUrl}")
    private String subjectUrl;
    @Value("${lessonPdfFileUrl}")
    private String lessonPdfFileUrl;

    @Resource
    private SubjectsService subjectsService;
    @Resource
    private SubjectLessonService subjectLessonService;
    @Resource
    private SubjectLessonVideoService subjectLessonVideoService;
    @Resource
    private SubjectLessonLogService subjectLessonLogService;
    @Resource
    private CdnUrlUtil cdnUrlUtil;


    @ApiOperation(value = "获取课次信息")
    @PostMapping(value = "/getById")
    public R<Object> getById(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        SubjectLessonDto lessonDto = subjectLessonService.dtoRelationByUid(uid);
        if (lessonDto == null) return R.fail("课次获取失败");
        if (!lessonDto.getEnabled().equals(1)) return R.fail("课次已被禁用");

        Subjects subjects = subjectsService.getById(lessonDto.getSubjectId());
        if (subjects == null) return R.fail("关联学科获取失败");
        if (!subjects.getEnabled().equals(1)) return R.fail("关联学科已被禁用");
        if (subjects.getFreeStatus() == null || !subjects.getFreeStatus().equals(1)) {
            // 非体验学科并且是体验账号，则没有有权限
            if (AccountUtil.isTyUser(corpUid)) {
                return R.fail("没有访问权限");
            }
        }

        String teachPlanUrl = lessonDto.getTeachPlanUrl();
        // 获取教案服务器认证地址
        if (StringUtils.isNotBlank(teachPlanUrl)) {
            lessonDto.setTeachPlanUrl(lessonPdfFileUrl.concat(teachPlanUrl));
        }
        // 获取课件服务器认证地址
        if (StringUtils.isNotBlank(lessonDto.getHtmlUrl())) {
            lessonDto.setHtmlUrl(subjectUrl.concat(lessonDto.getHtmlUrl()) + "?" + System.currentTimeMillis());
        }
        lessonDto.setDataUrl(null);

        // 课件访问记录
        String ip = IpUtil.getIpAddr(request);
        SubjectLessonLog log = SubjectUtil.saveLog(corpUid, storeUid, staffUid, staffName, ip, 1, lessonDto.getUid(), "");
        subjectLessonLogService.saveOrUpdate(log);
        return R.ok(lessonDto);
    }


    @ApiOperation(value = "获取课次资料的下载地址")
    @PostMapping(value = "/getDownloadUrl")
    public R<Object> getDownloadUrl(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        SubjectLessonDto lesson = subjectLessonService.dtoByUid(uid);
        if (lesson == null) return R.fail("课次获取失败");
        if (!lesson.getEnabled().equals(1)) return R.fail("课次已被禁用");

        // 判断门店学科的权限
        Subjects subjects = subjectsService.getById(lesson.getSubjectId());
        if (subjects == null) return R.fail("关联学科获取失败");
        if (!subjects.getEnabled().equals(1)) return R.fail("关联学科已被禁用");
        if (subjects.getFreeStatus() == null || !subjects.getFreeStatus().equals(1)) {
            // 非体验学科并且是体验账号，则没有有权限
            if (AccountUtil.isTyUser(corpUid)) {
                return R.fail("没有访问权限");
            }
        }

        String dataUrl = lesson.getDataUrl();
        if (StringUtils.isBlank(dataUrl))
            return R.fail("没有可下载的资料");

        // 防暴击下载
        R<Object> r = subjectLessonLogService.preventDuplicateDownload(storeUid, staffUid, uid, 2);
        if (R.isError(r))
            return r;

        // 下载资料记录
        String ip = IpUtil.getIpAddr(request);
        SubjectLessonLog log = SubjectUtil.saveLog(corpUid, storeUid, staffUid, staffName, ip, 2, lesson.getUid(), "");
        subjectLessonLogService.saveOrUpdate(log);
        return R.ok(subjectUrl.concat(dataUrl));
    }

    @ApiOperation(value = "获取课次的书籍信息")
    @PostMapping(value = "/getBookInfo")
    public R<Object> getBookInfo(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        SubjectLessonDto lesson = subjectLessonService.dtoByUid(uid);
        if (lesson == null)
            return R.fail("课次获取失败");

        BookDto bookDto = new BookDto();
        bookDto.setBookIsbn(lesson.getBookIsbn());
        bookDto.setBookPress(lesson.getBookPress());
        if (StringUtils.isNotBlank(lesson.getBookImg())) {
            R<String> r = cdnUrlUtil.getCdnUrl(request, lesson.getBookImg());
            if (R.isError(r))
                return R.fail(r.getMsg());

            bookDto.setBookImg(r.getData());
        }
        return R.ok(bookDto);
    }


    @ApiOperation(value = "获取课次的说课视频集合")
    @PostMapping(value = "/getVideoList")
    public R<Object> getVideoList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        SubjectLessonVideoListDto lesson = subjectLessonService.getVideoList(uid);
        return R.ok(lesson);
    }

    @ApiOperation(value = "获取课次的教研视频集合")
    @PostMapping(value = "/getTeachVideoList")
    public R<Object> getTeachVideoList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        return subjectLessonService.getTeachVideoList(uid);
    }

    @ApiOperation(value = "获取视频的观看地址")
    @PostMapping(value = "/getVideoUrl")
    public R<Object> getVideoUrl(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "videoUid") String videoUid
    ) {
        SubjectLessonVideoDto data = subjectLessonVideoService.dtoByUid(videoUid);
        if (data == null)
            return R.failGet();

        String lessonUid = data.getLessonUid();
        SubjectLessonDto lessonDto = subjectLessonService.dtoByUid(lessonUid);
        if (lessonDto == null)
            return R.failGet();

        R<String> r;
        if (lessonDto.getSubjectMainId().equals(346L) || lessonDto.getSubjectMainId().equals(353L) || lessonDto.getSubjectMainId().equals(381L)) {
            // 名师堂课件获取cdn链接
            r = cdnUrlUtil.getMstCdnUrl(request, data.getUrl());
        } else {
            // 叮叮课件获取cdn链接
            r = cdnUrlUtil.getCdnUrl(request, data.getUrl());
        }
        if (R.isError(r)) {
            return R.fail(r.getMsg());
        }
        String cdnUrl = r.getData();

        // 保存观看记录
        String ip = IpUtil.getIpAddr(request);
        SubjectLessonLog log = SubjectUtil.saveLog(corpUid, storeUid, staffUid, staffName, ip, 3, data.getLessonUid(), videoUid);
        subjectLessonLogService.saveOrUpdate(log);
        return R.ok(cdnUrl);
    }

}